// pages/patientPages/order/order.js
const ajax = require("../../../utils/ajax.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollWidth: 0,
    noticeText: "",
    dateIndex: 0,
    dateList: {},
    orderDetail: {},
    doctorId: "",
    notices: [],
    animationData: "",
    startX: 0,
    endX: 0,
    moveFlag: false,
    dateKeys: [],
    dateKeysIndex: 0
  },
  dateClick: function (e) {
    let index = e.currentTarget.dataset.index;
    console.log(index)
    this.setData({
      dateIndex: index,
      orderDetail: this.data.dateList[index]
    })
  },
  canSpecil(e) {
    let doctor_id = this.data.doctorId;
    let date = this.data.orderDetail.date;
    let times = e.currentTarget.dataset.times;
    ajax.request({
      url: app.url.isCanSpecial,
      data: {
        doctor_id: doctor_id,
        date: date,
        times: times
      }
    }).then(res => {
      if (res.code != 200) {
        return wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
      let money = res.money;
      wx.showModal({
        title: '提示',
        content: '支付￥' + money + '元,可提前订座哦',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/userPages/order/sureOrder/sureOrder?id=' + doctor_id + '&date=' + date + "&times=" + times
            })
          } else if (res.cancel) {

          }
        }
      })
    })
  },

  touchStart: function (e) {
    let startX = e.touches[0].pageX; // 获取触摸时的原点
    let moveFlag = true;
    this.setData({
      startX: startX,
      moveFlag: moveFlag
    })
    console.log(e)
  },
  // 触摸移动事件
  touchMove: function (e) {
    let endX = e.touches[0].pageX; // 获取触摸时的原点
    let startX = this.data.startX;
    let moveFlag = this.data.moveFlag
    if (moveFlag) {
      if (endX - startX > 50) {
        console.log("move right");
        this.move2right();
        moveFlag = false;
      }
      if (startX - endX > 50) {
        console.log("move left");
        this.move2left();
        moveFlag = false;
      }
    }
    this.setData({
      moveFlag: moveFlag
    });
  },
  move2right() {
    let index = parseInt(this.data.dateKeysIndex)
    if (index == 0) {
      return;
    }
    let i = parseInt(index) - 1;
    let ind = this.data.dateKeys[i]
    console.log(ind)
    this.setData({
      dateKeysIndex: i,
      dateIndex: ind,
      orderDetail: this.data.dateList[ind]
    })
  },
  move2left() {
    let index = parseInt(this.data.dateKeysIndex)
    if (index + 1 > this.data.dateList.lenght) {
      return;
    }
    let i = parseInt(index) + 1;
    console.log(i)
    console.log(this.data.dateKeys)
    let ind = this.data.dateKeys[i]
    console.log(ind)
    this.setData({
      dateKeysIndex: i,
      dateIndex: ind,
      orderDetail: this.data.dateList[ind]
    })
  },
  // 触摸结束事件
  touchEnd: function (e) {
    let moveFlag = true; // 回复滑动事件    
  },
  orderClick: function (e) {
    let doctor_id = this.data.doctorId;
    let date = this.data.orderDetail.date;
    let times = e.currentTarget.dataset.times;
    ajax.request({
      url: app.url.isCanSpecial,
      data: {
        doctor_id: doctor_id,
        date: date,
        times: times
      }
    }).then(res => {

      if (res.code == 200) {
        let money = res.money;
        wx.showModal({
          title: '提示',
          content: '支付￥' + money + '元,可提前订座哦',
          success: function (res) {
            if (res.confirm) {
              wx.showToast({
                title: '订单暂未开通',
                icon: 'none'
              })
            } else if (res.cancel) {
            }
          }
        })
      } else if (res.code == 400) {
        wx.navigateTo({
          url: '/pages/userPages/order/sureOrder/sureOrder?id=' + doctor_id + '&date=' + date + "&times=" + times,
        })
      } else {
        return wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }

    })

  },
  scrollText: function () {
    let length = "";
    const query = wx.createSelectorQuery().in(this)
    query.select('#notice').boundingClientRect(function (res) {
      length = res.width
    }).exec()
    let scrollWidth = 0
    let timer = setInterval(() => {
      scrollWidth += 1;
      let animation = wx.createAnimation()
      animation.translateX(-scrollWidth).step({ duration: 0, timingFunction: 'linear' })
      this.setData({
        animationData: animation.export()
      })
      if (scrollWidth > length) {
        scrollWidth = -length
      }
    }, 30)
    return timer;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      doctorId: options.id
    })    
    // this.getDoctorSits()
  },
  getDoctorSits() {
    let id = this.data.doctorId;
    wx.showLoading({
      title: '拼命加载中..',
    })
    ajax.request({
      url: app.url.doctorSits,
      data: {
        doctor_id: id
      }
    }).then(res => {
      if (res.code !== 200) {
        wx.hideLoading()
        return wx.showModal({
          title: '提示',
          content: res.msg,
          success(res) {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
      this.setData({
        dateList: res.data.list,
        dateKeys: Object.keys(res.data.list),
        dateIndex: Object.keys(res.data.list)[0],
        orderDetail: res.data.list[Object.keys(res.data.list)[0]],
        notices: res.data.notices
      })

      this.parserNoticeStr();
      wx.hideLoading()
      this.scrollText();
    })
  },
  parserNoticeStr() {
    var str = '';
    for (var i in this.data.notices) {
      str += ' ' + this.data.notices[i].content
    }
    this.setData({
      noticeText: str
    })
    let length = str.length * 25
    this.setData({
      scrollWidth: -length
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDoctorSits()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let timer = this.scrollText()
    clearInterval(timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})